//
//  AppCLLocation.m
//  Speedater
//
//  Created by Ngo Thang on 1/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppCLLocation.h"
#import "AppDelegate.h"

@implementation AppCLLocation

@synthesize locationManager;
@synthesize degreeCompass;

- (void)onUpdateHeading:(float)mHeading
{
    NSInteger degree = 0;
    if ((mHeading >= 339) || (mHeading <= 22)) {
        degree = 0;
    }else if ((mHeading > 23) && (mHeading <= 68)) {
        degree = 45;
    }else if ((mHeading > 69) && (mHeading <= 113)) {
        degree = 90;
    }else if ((mHeading > 114) && (mHeading <= 158)) {
        degree = 135;
    }else if ((mHeading > 159) && (mHeading <= 203)) {
        degree = 180;
    }else if ((mHeading > 204) && (mHeading <= 248)) {
        degree = 225;
    }else if ((mHeading > 249) && (mHeading <= 293)) {
        degree = 270;
    }else if ((mHeading > 294) && (mHeading <= 338)) {
        degree = 315;
    }
    
    if (degree != degreeCompass) {
        degreeCompass = degree;
        [[NSNotificationCenter defaultCenter] postNotificationName: @"degreeCompassChanged" 
                                                            object: self 
                                                          userInfo:nil];
    }
}

- (void)updateHeadingPsudo
{
    float mHeading = arc4random() % 360;
    [self onUpdateHeading:mHeading];
}

- (id)init
{
    self = [super init];
    if (self) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;

        float sysVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (![CLLocationManager locationServicesEnabled] && sysVersion < 5.0) {
            UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] 
                                                  initWithTitle:NSLocalizedString(@"Location Services Disabled", nil) 
                                                  message:NSLocalizedString(@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled.", nil)
                                                  delegate:nil 
                                                  cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                  otherButtonTitles:nil];
            [servicesDisabledAlert show];
            [servicesDisabledAlert release];
        }
        
        [self.locationManager startUpdatingLocation];
        [self.locationManager startUpdatingHeading];
        
        //[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(updateHeadingPsudo) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)dealloc
{
    [locationManager release];
    [super dealloc];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    AppDelegate *d = [[UIApplication sharedApplication] delegate];
    d.lat = newLocation.coordinate.latitude;
    d.lon = newLocation.coordinate.longitude;
    
    // store
    [[NSUserDefaults standardUserDefaults] setFloat:d.lat forKey:@"gps_lat"];
    [[NSUserDefaults standardUserDefaults] setFloat:d.lon forKey:@"gps_lon"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    //NSLog(@"GPS(%f, %f)", d.lat, d.lon);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    //NSLog(@"%.2f degrees", newHeading.magneticHeading);
    
    float mHeading = newHeading.trueHeading;
    [self onUpdateHeading:mHeading];
    
}

@end
